<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
// 将map按照时间字段值降序
	class MapComparator implements Comparator<Map> {
		@Override
		public int compare(Map map1, Map map2) {
			double time1 = 0;
			double time2 = 0;
			try {

				time1 = Double.parseDouble(Tools.getDesc(map1.get("ratio1").toString()));
				time2 = Double.parseDouble(Tools.getDesc(map2.get("ratio1").toString()));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return (time1 > time2) ? -1 : (time1 == time2) ? 0 : 1;
		}
	}
public Map getMap(List<Map> result , Map<String,String> deptMap ){
		
		int sumCount = 0 ;
		//处理结合数据
		for(Map map : result){
			String ksdm = map.get("ksdm") == null ? "0" : map.get("ksdm").toString();
			map.put("deptName",deptMap.get(ksdm));
			map.put("cont",map.get("cont").toString()+"");
			
			sumCount++;
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		resMap.put("sumCount",sumCount);
		return resMap;
	}
%>
<%
	//查询医保自费比的科室排名
	String yydm =  input.getDesc("yydm","01");//医院代码	
	String ksdm = input.getDesc("ksdm","-1"); //传入科室代码代表该科室主任
	String date = input.getDesc("start_date","2018-01-11");//开始时间
	String endDa = input.getDesc("end_date","2018-01-11");//结束时间

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	//返回参数
	DecimalFormat df = new DecimalFormat("######0.00"); 
	Message msg = new Message();
	
	
		
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept ");
	Map<String,String> deptMap1 = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap1.put(map.get("id").toString(),map.get("name").toString());
	}
	
	
	//当前使用数量
	double currentCount = 0;
	String sumCount = "";
	String sqlWhere = "";
	String hql4Where = " ";
	if(!ksdm.equals("-1")){
		sqlWhere += " and a.ksdm in(" + ksdm + ")";
		
	}
	if(!yydm.equals("-1"))
	sqlWhere += " and a.ksdm in (select id  from his_dept where yydm ='"+yydm+"') " ;
	//在院的医保人数
	String hql3 = "select new map(count(a) as count , a.ksdm as ksdm ) from ZY_BRSYK a where a.ybdm != '101' and brzt !=9 and rqrq != '' and cwdm !='' and cwdm is not null and (( cyrq >= '" + endDate + "' and rqrq <= '" + endDate + "' ) or (cyrq is null and rqrq <= '" + endDate + "')) "+ sqlWhere +" group by a.ksdm  order by count(a) desc";
	List<Map> result3 = QBuilder.db("basedb").queryHql(hql3);
	
	Map ybMap = new HashMap();
	for(Map map : result3){
		ybMap.put(map.get("ksdm"),map.get("count"));
	}
	//在院的非医保人数
	String hql4 = "select new map(count(a) as count , a.ksdm as ksdm ) from ZY_BRSYK a where a.ybdm = '101' and brzt !=9 and rqrq != '' and cwdm !='' and cwdm is not null and (( cyrq >= '" + endDate + "' and rqrq <= '" + endDate + "' ) or (cyrq is null and rqrq <= '" + endDate + "')) "+ sqlWhere +" group by a.ksdm order by count(a) desc";
	List<Map> result4 = QBuilder.db("basedb").queryHql(hql4);
	Map zfMap = new HashMap();
	for(Map map : result4){
		zfMap.put(map.get("ksdm"),map.get("count"));
	}
	
	List<Map> resList = new ArrayList();
	
	
	for(Map map : deptList){
		String mksdm = map.get("id") == null ? "" :  map.get("id").toString();
		
		int zfCount = zfMap.get(mksdm) == null ? 0 :  Integer.parseInt( zfMap.get(mksdm).toString());
		int ybCount = ybMap.get(mksdm) == null ? 0 :  Integer.parseInt( ybMap.get(mksdm).toString());
		if(zfCount ==0 && ybCount ==0){
			continue;
		}
		double num1 = ybCount;
		double num2 = zfCount;
		if(zfCount == 0){
			map.put("cont",df.format(ybCount)+"——"+ybCount+":"+zfCount);
			map.put("ratio1",df.format(ybCount));
		}else{
			map.put("cont",df.format(num1/num2)+"——"+ybCount+":"+zfCount);
			map.put("ratio1",df.format(num1/num2));
		}
	
		
		map.put("ksdm",mksdm);
		resList.add(map);
	}
	/*
	//遍历科室
	for(Map map : result3){
		String mksdm = map.get("ksdm") == null ? "" :  map.get("ksdm").toString();
		
		int zfCount = zfMap.get(mksdm) == null ? 0 :  Integer.parseInt( zfMap.get(mksdm).toString());
		int ybCount = ybMap.get(mksdm) == null ? 0 :  Integer.parseInt( ybMap.get(mksdm).toString());
		double num1 = ybCount;
		double num2 = zfCount;
		if(zfCount == 0){
			map.put("cont",df.format(ybCount)+"——"+ybCount+":"+zfCount);
		}else{
			map.put("cont",df.format(num1/num2)+"——"+ybCount+":"+zfCount);
		}
	
		map.put("ratio1",df.format(num1/num2));
		
	}
*/
	
	Collections.sort(resList, new MapComparator());
	Map<String,Object> m1 = new HashMap<String,Object>();
	
	m1.put("ksMap",getMap(resList , deptMap1 ));
	msg.put("errcode",0);
	msg.put("ybzfb",m1);

	
	
	
	out.println(msg);
%>